package com.dengke.hellospringmvc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class OtherExamplecontroller {

	@RequestMapping("/request")
	public String requestResponseExample(HttpServletRequest request,HttpServletResponse response,Model model) {
	
		return "";
	}
	
	@RequestMapping(value="/redirect",method=RequestMethod.GET)
	public String authorInfo(Model model){
		return "redirect:/hello";
	}
	
	@RequestMapping("/user")
	public String userInfo(Model model,@RequestParam(value="name",defaultValue="Guest") String name) {
		model.addAttribute("name", name);
		if("admin".equals(name)){
			model.addAttribute("email", "admin@example.com");
		}else {
			model.addAttribute("email", "Not Set");
		}
		return "userInfo";
	}
	
	@RequestMapping("/web/fe/{sitePrefix}/{language}/document/{id}/{naturalText}")
	public String docmentView(Model model,
			@PathVariable(value="sitePrefix") String sitePrefix,
			@PathVariable(value="language") String language,
			@PathVariable(value="id") Long id,
			@PathVariable(value="naturalText") String naturalText) {
		model.addAttribute("sitePrefix", sitePrefix);
		model.addAttribute("language", language);
		model.addAttribute("id", id);
		model.addAttribute("naturalText", naturalText);
		
		 String documentName = "Java tutorial for Beginners";
	      if (id == 8108) {
	          documentName = "Spring MVC for Beginners";
	      }
	 
	      model.addAttribute("documentName", documentName);
	 
	      return "documentView";
	}
	
	@RequestMapping(value="/savaResult")
	@ResponseBody
	public String saveResponse(Model model) {
		return "saved";
	}
}
